package com.example.javamediaserver.config;

import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@RequiredArgsConstructor
public class WebConfig implements WebMvcConfigurer {

    private final MediaServerConfig mediaServerConfig;
    
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 配置录制文件静态访问
        registry.addResourceHandler("/recordings/**")
                .addResourceLocations("file:" + mediaServerConfig.getRecordings().getAbsolutePath() + "/")
                .setCachePeriod(3600);
        
        // 配置HLS文件静态访问
        registry.addResourceHandler("/live/**")
                .addResourceLocations("file:" + mediaServerConfig.getHls().getAbsolutePath() + "/")
                .setCachePeriod(0); // HLS文件不缓存
    }
    
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                .maxAge(3600);
    }
} 